*Need age (PR), citizenship(demographics), medicaid/insurance (insurance), SSI receipt (inc/assets)

global temp "R:\SharedProjects\Shared2020-029\temp\"
global output "R:\SharedProjects\Shared2020-029\output\"
global geo "R:\Restricted\Geographic Information\xyrState\stata"
global hrs2008 "R:\Public\2008hrs\Final\Core\stata"
global randinc "R:\Public\Contributions\Rand\RandIncWlthImp\stata"
global hrs2010 "R:\Public\2010hrs\Final\Core\stata"
global hrs2012 "R:\Public\2012hrs\Final\Core\stata"
global hrs2014 "R:\Public\2014hrs\Final\Core\stata"
global hrs2016 "R:\Public\2016hrs\Final\Core\stata"
global hrs2018 "R:\Public\2018hrs\Early\Core\stata"
global tracker "R:\Public\Trk2016\stata"

set maxvar 15000

**Pull weights from tracker file

use "$tracker\TRK2016TR_R.dta",clear
rename LWGTR wgt2008
rename MWGTR wgt2010
rename NWGTR wgt2012
rename OWGTR wgt2014
rename PWGTR wgt2016
keep HHID PN wgt2008 wgt2010 wgt2012 wgt2014 wgt2016
save $temp\weights.dta, replace


use "$randinc\randhrsimp1992_2016v2.dta", clear
rename hhid HHID
rename pn PN
keep *itot* HHID PN
save $temp/income.dta, replace

use "$geo\HRSXSTATE16.dta", clear
rename hhid HHID
rename pn PN
keep STFIPS08 STFIPS10 STFIPS12 STFIPS14 STFIPS16 HHID PN
save $temp/states.dta, replace


***2008
/*
use "$hrs2008i\incwlth08f1b.dta", clear
rename h9itot hhincome 
rename hhid HHID
rename pn PN
keep hhincome HHID PN
save "$temp\hrs08_inc.dta", replace
*/

use "$hrs2008\H08PR_R.dta", clear
rename LX067_R yob
gen age2014=2014-yob
gen citizen=0
replace citizen=1 if LZ230==1
rename LZ216 educ 
keep yob age2014 citizen educ PN HHID
save "$temp\hrs08_demo.dta", replace

use "$hrs2008\H08B_R.dta", clear
gen citizen_n=0
replace citizen_n=1 if LB085==1
replace citizen_n=1 if LB086==1
rename LB014 educ_i
rename LB015 hs_i
keep citizen_n educ_i hs_i PN HHID
save "$temp\hrs08_demo2.dta", replace

use "$hrs2008\H08Q_H.dta", clear
tab LQ106
gen ssi=0
replace ssi=1 if LQ106 < 4
rename LPN_CS PN
keep HHID PN ssi
save "$temp\hrs08_ssi.dta", replace

use "$hrs2008\H08N_R.dta", clear
gen mcaid=0
replace mcaid=1 if LN006==1
gen mcaidprev2=0
replace mcaidprev2=1 if LN005==1
gen uninsuredprev2=0
replace uninsuredprev2=1 if LN091==1
gen uninsured=0
replace uninsured=1 if LN342==1
keep PN HHID mcaid mcaidprev2 uninsuredprev2 uninsured
save "$temp\hrs08_ins.dta", replace
merge 1:1 PN HHID using $temp\hrs08_demo.dta, nogen
merge 1:1 PN HHID using $temp\hrs08_demo2.dta, nogen
merge 1:1 PN HHID using $temp\hrs08_ssi.dta, nogen
*merge 1:1 PN HHID using $temp\hrs08_inc.dta, nogen
merge 1:1 PN HHID using $temp\income.dta, nogen keep(3)
merge 1:1 PN HHID using $temp\states.dta, nogen keep(3)
gen state=STFIPS08
gen surveyyear=2008
summ h9itot 
bys HHID: egen famssi=max(ssi)
bys HHID: gen numhhmem=_N
preserve
gen in2008=0
keep HHID PN in2008
save "$temp/in2008.dta", replace
restore 
gen hsdegree=0
replace hsdegree=1 if educ >= 12
replace hsdegree=1 if educ_i > 11 & educ_i!=97 & educ_i!=.
replace hsdegree=1 if hs_i==1
replace citizen=1 if citizen_n==1
keep if age2014 > 54 & age2014 < 65
keep if citizen==1
keep if famssi==0
gen keepit=0
replace keepit=1 if hsdegree==0
**Code 2008 poverty level from ASPE website:
replace keepit=1 if numhhmem==1 & h9itot < 10400*1.38
replace keepit=1 if numhhmem==2 & h9itot < 14000*1.38
replace keepit=1 if numhhmem==3 & h9itot < 17600*1.38
replace keepit=1 if numhhmem==4 & h9itot < 21200*1.38
replace keepit=1 if numhhmem==5 & h9itot < 24800*1.38
keep if keepit==1
drop keepit
drop *STFIPS*
save "$temp/hrs2008.dta", replace

preserve
rename mcaid mcaid2008
rename uninsured uninsured2008
rename mcaidprev2 mcaid20062008
rename uninsuredprev2 unins20062008
gen insamp08=1
keep mcaid2008 uninsured2008 mcaid20062008 unins20062008 HHID PN insamp08
save $temp/insvars2008.dta, replace
restore

merge 1:1 HHID PN using $temp/in2008.dta
replace in2008=1 if _merge==3
keep HHID PN in2008
save "$temp/in2008.dta", replace

***2010 wave

use "$hrs2010\H10PR_R.dta", clear
rename MX067_R yob
gen age2014=2014-yob
gen citizen=0
replace citizen=1 if MZ230==1
replace citizen=. if MZ230==.
rename MZ216 educ 
keep yob age2014 citizen educ PN HHID
save "$temp\hrs10_demo.dta", replace

use "$hrs2010\H10B_R.dta", clear
gen citizen_n=0
replace citizen_n=1 if MB085==1
replace citizen_n=1 if MB086==1
rename MB014 educ_i
rename MB015 hs_i
keep citizen_n educ_i hs_i PN HHID
save "$temp\hrs10_demo2.dta", replace

use "$hrs2010\H10Q_H.dta", clear
tab MQ106
gen ssi=0
replace ssi=1 if MQ106 < 4
rename MPN_CS PN
keep HHID PN ssi
save "$temp\hrs10_ssi.dta", replace

use "$hrs2010\H10N_R.dta", clear
gen mcaid=0
replace mcaid=1 if MN006==1
gen mcaidprev2=0
replace mcaidprev2=1 if MN005==1
gen uninsuredprev2=0
replace uninsuredprev2=1 if MN091==1
gen uninsured=0
replace uninsured=1 if MN342==1
keep PN HHID mcaid mcaidprev2 uninsuredprev2 uninsured
save "$temp\hrs10_ins.dta", replace
merge 1:1 PN HHID using $temp\hrs10_demo.dta, nogen
merge 1:1 PN HHID using $temp\hrs10_demo2.dta, nogen
merge 1:1 PN HHID using $temp\hrs10_ssi.dta, nogen
merge 1:1 PN HHID using $temp\income.dta, nogen keep(3)
merge 1:1 PN HHID using $temp\in2008.dta, nogen keep(1 3)
merge 1:1 PN HHID using $temp\states.dta, nogen keep(3)
gen state=STFIPS10
gen surveyyear=2010
bys HHID: egen famssi=max(ssi)
bys HHID: gen numhhmem=_N
preserve
gen in2010=0
keep HHID PN in2010
save "$temp/in2010.dta", replace
restore 
gen hsdegree=0
replace hsdegree=1 if educ >= 12
replace hsdegree=1 if educ_i > 11 & educ_i!=97 & educ_i!=.
replace hsdegree=1 if hs_i==1
replace citizen=1 if citizen_n==1
keep if age2014 > 54 & age2014 < 65
drop if in2008==. & famssi!=0
drop if in2008==. & citizen==0
gen keepit=0
replace keepit=1 if hsdegree==0
**Code 2010 poverty level from ASPE website:
replace keepit=1 if numhhmem==1 & h10itot < 10830*1.38
replace keepit=1 if numhhmem==2 & h10itot < 14570*1.38
replace keepit=1 if numhhmem==3 & h10itot < 18310*1.38
replace keepit=1 if numhhmem==4 & h10itot < 22050*1.38
replace keepit=1 if numhhmem==5 & h10itot < 25790*1.38
replace keepit=1 if in2008==1
replace keepit=0 if in2008==0
keep if keepit==1
drop keepit
drop *STFIPS*
save "$temp/hrs2010.dta", replace


preserve
rename mcaid mcaid2010
rename uninsured uninsured2010
rename mcaidprev2 mcaid20082010
rename uninsuredprev2 unins20082010
gen insamp10=1
keep mcaid2010 uninsured2010 mcaid20082010 unins20082010 HHID PN insamp10
save $temp/insvars2010.dta, replace
restore


merge 1:1 HHID PN using $temp/in2010.dta
replace in2010=1 if _merge==3
keep HHID PN in2010
save "$temp/in2010.dta", replace


***2012 wave
use "$hrs2012\H12PR_R.dta", clear
rename NX067_R yob
gen age2014=2014-yob
gen citizen=0
replace citizen=1 if NZ230==1
replace citizen=. if NZ230==.
rename NZ216 educ 
keep yob age2014 citizen educ PN HHID
save "$temp\hrs12_demo.dta", replace

use "$hrs2012\H12B_R.dta", clear
gen citizen_n=0
replace citizen_n=1 if NB085==1
replace citizen_n=1 if NB086==1
rename NB014 educ_i
rename NB015 hs_i
keep citizen_n educ_i hs_i PN HHID
save "$temp\hrs12_demo2.dta", replace

use "$hrs2012\H12Q_H.dta", clear
tab NQ106
gen ssi=0
replace ssi=1 if NQ106 < 4
rename NPN_CS PN
keep HHID PN ssi
save "$temp\hrs12_ssi.dta", replace

use "$hrs2012\H12N_R.dta", clear
gen mcaid=0
replace mcaid=1 if NN006==1
gen mcaidprev2=0
replace mcaidprev2=1 if NN005==1
gen uninsuredprev2=0
replace uninsuredprev2=1 if NN091==1
gen uninsured=0
replace uninsured=1 if NN342==1
keep PN HHID mcaid mcaidprev2 uninsuredprev2 uninsured
save "$temp\hrs12_ins.dta", replace
merge 1:1 PN HHID using $temp\hrs12_demo.dta, nogen
merge 1:1 PN HHID using $temp\hrs12_demo2.dta, nogen
merge 1:1 PN HHID using $temp\hrs12_ssi.dta, nogen
merge 1:1 PN HHID using $temp\income.dta, nogen keep(3)
merge 1:1 PN HHID using $temp\in2010.dta, nogen keep(1 3)
merge 1:1 PN HHID using $temp\states.dta, nogen keep(3)
gen state=STFIPS12
bys HHID: egen famssi=max(ssi)
bys HHID: gen numhhmem=_N
preserve
gen in2012=0
keep HHID PN in2012
save "$temp/in2012.dta", replace
restore 
gen surveyyear=2012
gen hsdegree=0
replace hsdegree=1 if educ >= 12
replace hsdegree=1 if educ_i > 11 & educ_i!=97 & educ_i!=.
replace hsdegree=1 if hs_i==1
replace citizen=1 if citizen_n==1
keep if age2014 > 54 & age2014 < 65
drop if in2010==. & famssi!=0
drop if in2010==. & citizen==0
gen keepit=0
replace keepit=1 if hsdegree==0
**Code 2012 poverty level from ASPE website:
replace keepit=1 if numhhmem==1 & h11itot < 11170*1.38
replace keepit=1 if numhhmem==2 & h11itot < 15130*1.38
replace keepit=1 if numhhmem==3 & h11itot < 19090*1.38
replace keepit=1 if numhhmem==4 & h11itot < 23050*1.38
replace keepit=1 if numhhmem==5 & h11itot < 27010*1.38
replace keepit=1 if in2010==1
replace keepit=0 if in2010==0
keep if keepit==1
drop keepit
drop *STFIPS*
save "$temp/hrs2012.dta", replace

preserve
rename mcaid mcaid2012
rename uninsured uninsured2012
rename mcaidprev2 mcaid20102012
rename uninsuredprev2 unins20102012
gen insamp12=1
keep mcaid2012 uninsured2012 mcaid20102012 unins20102012 HHID PN insamp12
save $temp/insvars2012.dta, replace
restore

merge 1:1 HHID PN using $temp/in2012.dta
replace in2012=1 if _merge==3
keep HHID PN in2012
save "$temp/in2012.dta", replace

***2014 wave

use "$hrs2014\H14PR_R.dta", clear
rename OX067_R yob
gen age2014=2014-yob
gen citizen=0
replace citizen=1 if OZ230==1
replace citizen=. if OZ230==.
rename OZ216 educ 
keep yob age2014 citizen educ PN HHID
summ
save "$temp\hrs14_demo.dta", replace

use "$hrs2014\H14B_R.dta", clear
gen citizen_n=0
replace citizen_n=1 if OB085==1
replace citizen_n=1 if OB086==1
rename OB014 educ_i
rename OB015 hs_i
keep citizen_n educ_i hs_i PN HHID
summ
save "$temp\hrs14_demo2.dta", replace

use "$hrs2014\H14Q_H.dta", clear
tab OQ106
gen ssi=0
replace ssi=1 if OQ106 < 4
rename OPN_CS PN
keep HHID PN ssi
save "$temp\hrs14_ssi.dta", replace

use "$hrs2014\H14N_R.dta", clear
gen mcaid=0
replace mcaid=1 if ON006==1
gen mcaidprev2=0
replace mcaidprev2=1 if ON005==1
gen uninsuredprev2=0
replace uninsuredprev2=1 if ON091==1
gen uninsured=0
replace uninsured=1 if ON342==1
keep PN HHID mcaid mcaidprev2 uninsuredprev2 uninsured
save "$temp\hrs14_ins.dta", replace
merge 1:1 PN HHID using $temp\hrs14_demo.dta, nogen
merge 1:1 PN HHID using $temp\hrs14_demo2.dta, nogen
merge 1:1 PN HHID using $temp\hrs14_ssi.dta, nogen
merge 1:1 PN HHID using $temp\income.dta, nogen keep(3)
merge 1:1 PN HHID using $temp\in2012.dta, nogen keep(1 3)
merge 1:1 PN HHID using $temp\states.dta, nogen keep(3)
gen state=STFIPS14
bys HHID: egen famssi=max(ssi)
bys HHID: gen numhhmem=_N
preserve
gen in2014=0
keep HHID PN in2014
save "$temp/in2014.dta", replace
restore 
gen surveyyear=2014
gen hsdegree=0
replace hsdegree=1 if educ >= 12
replace hsdegree=1 if educ_i > 11 & educ_i!=97 & educ_i!=.
replace hsdegree=1 if hs_i==1
replace citizen=1 if citizen_n==1
keep if age2014 > 54 & age2014 < 65
drop if in2012==. & famssi!=0
drop if in2012==. & citizen==0
gen keepit=0
replace keepit=1 if hsdegree==0
**Code 2012 poverty level from ASPE website:
replace keepit=1 if numhhmem==1 & h12itot < 11670*1.38
replace keepit=1 if numhhmem==2 & h12itot < 15730*1.38
replace keepit=1 if numhhmem==3 & h12itot < 19790*1.38
replace keepit=1 if numhhmem==4 & h12itot < 23850*1.38
replace keepit=1 if numhhmem==5 & h12itot < 27910*1.38
replace keepit=1 if in2012==1
replace keepit=0 if in2012==0
keep if keepit==1
drop keepit
drop *STFIPS*
save "$temp/hrs2014.dta", replace

preserve
rename mcaid mcaid2014
rename uninsured uninsured2014
rename mcaidprev2 mcaid20122014
rename uninsuredprev2 unins20122014
gen insamp14=1
keep mcaid2014 uninsured2014 mcaid20122014 unins20122014 HHID PN insamp14
save $temp/insvars2014.dta, replace
restore

merge 1:1 HHID PN using $temp/in2014.dta
replace in2014=1 if _merge==3
keep HHID PN in2014
save "$temp/in2014.dta", replace

***2016 wave

use "$hrs2016\H16PR_R.dta", clear
rename PX067_R yob
gen age2014=2014-yob
gen citizen=0
replace citizen=1 if PZ230==1
replace citizen=. if PZ230==.
rename PZ216 educ 
keep yob age2014 citizen educ PN HHID
summ
save "$temp\hrs16_demo.dta", replace

use "$hrs2016\H16B_R.dta", clear
gen citizen_n=0
replace citizen_n=1 if PB085==1
replace citizen_n=1 if PB086==1
rename PB014 educ_i
rename PB015 hs_i
keep citizen_n educ_i hs_i PN HHID
summ
save "$temp\hrs16_demo2.dta", replace

use "$hrs2016\H16Q_H.dta", clear
tab PQ106
gen ssi=0
replace ssi=1 if PQ106 < 4
rename PPN_CS PN
keep HHID PN ssi
save "$temp\hrs16_ssi.dta", replace

use "$hrs2016\H16N_R.dta", clear
gen mcaid=0
replace mcaid=1 if PN006==1
gen mcaidprev2=0
replace mcaidprev2=1 if PN005==1
gen uninsuredprev2=0
replace uninsuredprev2=1 if PN091==1
gen uninsured=0
replace uninsured=1 if PN342==1
keep PN HHID mcaid mcaidprev2 uninsuredprev2 uninsured
save "$temp\hrs16_ins.dta", replace
merge 1:1 PN HHID using $temp\hrs16_demo.dta, nogen
merge 1:1 PN HHID using $temp\hrs16_demo2.dta, nogen
merge 1:1 PN HHID using $temp\hrs16_ssi.dta, nogen
merge 1:1 PN HHID using $temp\income.dta, nogen keep(3)
merge 1:1 PN HHID using $temp\in2014.dta, nogen keep(1 3)
merge 1:1 PN HHID using $temp\states.dta, nogen keep(3)
gen state=STFIPS16
bys HHID: egen famssi=max(ssi)
bys HHID: gen numhhmem=_N
gen surveyyear=2016
preserve
gen in2016=0
keep HHID PN in2016
save "$temp/in2016.dta", replace
restore 
gen hsdegree=0
replace hsdegree=1 if educ >= 12
replace hsdegree=1 if educ_i > 11 & educ_i!=97 & educ_i!=.
replace hsdegree=1 if hs_i==1
replace citizen=1 if citizen_n==1
keep if age2014 > 54 & age2014 < 65
drop if in2014==. & famssi!=0
drop if in2014==. & citizen==0
gen keepit=0
replace keepit=1 if hsdegree==0
**Code 2012 poverty level from ASPE website:
replace keepit=1 if numhhmem==1 & h13itot < 11880*1.38
replace keepit=1 if numhhmem==2 & h13itot < 16020*1.38
replace keepit=1 if numhhmem==3 & h13itot < 20160*1.38
replace keepit=1 if numhhmem==4 & h13itot < 24300*1.38
replace keepit=1 if numhhmem==5 & h13itot < 28440*1.38
replace keepit=1 if in2014==1
replace keepit=0 if in2014==0
keep if keepit==1
drop keepit
drop *STFIPS*
save "$temp/hrs2016.dta", replace

preserve
rename mcaid mcaid2016
rename uninsured uninsured2016
rename mcaidprev2 mcaid20142016
rename uninsuredprev2 unins20142016
gen insamp16=1
keep mcaid2016 uninsured2016 mcaid20142016 unins20142016 HHID PN insamp16
save $temp/insvars2016.dta, replace
restore

merge 1:1 HHID PN using $temp/in2016.dta
replace in2016=1 if _merge==3
keep HHID PN in2016
save "$temp/in2016.dta", replace

***2018 wave

use "$hrs2018\H18PR_R.dta", clear
rename QX067_R yob
gen age2014=2014-yob
gen citizen=0
replace citizen=1 if QZ230==1
replace citizen=. if QZ230==.
rename QZ216 educ 
keep yob age2014 citizen educ PN HHID
summ
save "$temp\hrs18_demo.dta", replace

use "$hrs2018\H18B_R.dta", clear
gen citizen_n=0
replace citizen_n=1 if QB085==1
replace citizen_n=1 if QB086==1
rename QB014 educ_i
rename QB015 hs_i
keep citizen_n educ_i hs_i PN HHID
summ
save "$temp\hrs18_demo2.dta", replace

use "$hrs2018\H18Q_H.dta", clear
tab QQ106
gen ssi=0
replace ssi=1 if QQ106 < 4
rename QPN_CS PN
keep HHID PN ssi
save "$temp\hrs18_ssi.dta", replace

use "$hrs2018\H18N_R.dta", clear
gen mcaid=0
replace mcaid=1 if QN006==1
gen mcaidprev2=0
replace mcaidprev2=1 if QN005==1
gen uninsuredprev2=0
replace uninsuredprev2=1 if QN091==1
gen uninsured=0
replace uninsured=1 if QN342==1
keep PN HHID mcaid mcaidprev2 uninsuredprev2 uninsured
save "$temp\hrs18_ins.dta", replace
merge 1:1 PN HHID using $temp\hrs18_demo.dta, nogen
merge 1:1 PN HHID using $temp\hrs18_demo2.dta, nogen
merge 1:1 PN HHID using $temp\hrs18_ssi.dta, nogen
merge 1:1 PN HHID using $temp\income.dta, nogen keep(3)
merge 1:1 PN HHID using $temp\in2016.dta, nogen keep(1 3)
merge 1:1 PN HHID using $temp\states.dta, nogen keep(3)
gen state=STFIPS16
drop if state==""
keep if in2016==1
gen surveyyear=2017
preserve
gen in2018=0
keep HHID PN in2018
save "$temp/in2018.dta", replace
restore 
drop *STFIPS*
save "$temp/hrs2018.dta", replace

preserve
rename mcaid mcaid2018
rename uninsured uninsured2018
rename mcaidprev2 mcaid20162018
rename uninsuredprev2 unins20162018
gen insamp18=1
keep mcaid2018 uninsured2018 mcaid20162018 unins20162018 HHID PN insamp18
save $temp/insvars2018.dta, replace
restore


use $temp/hrs2016.dta, clear
append using $temp/hrs2014.dta $temp/hrs2012.dta $temp/hrs2010.dta $temp/hrs2008.dta $temp/hrs2018.dta

merge m:1 HHID PN using $temp/insvars2018, nogen
merge m:1 HHID PN using $temp/insvars2016, nogen
merge m:1 HHID PN using $temp/insvars2014, nogen
merge m:1 HHID PN using $temp/insvars2012, nogen
merge m:1 HHID PN using $temp/insvars2010, nogen
merge m:1 HHID PN using $temp/insvars2008, nogen
merge m:1 HHID PN using $temp/weights, nogen keep(3)

destring state, replace
gen treated14=inlist(state,4,5,6,8,9,10,11,15,17,19,21,24,25,26,27,32,34,35,36,38,39, 41,44,50,53,54)
gen treated14b=state==33
gen treated15a=inlist(state,18,42)
gen treated15b=state==2
gen treated16=inlist(state, 30)
gen expyear=.
replace expyear=2014 if treated14==1
replace expyear=2015 if treated15a|treated14b
replace expyear=2016 if treated15b|treated16
gen expand=1*(treated14|treated14b|treated15a|treated15b|treated16)
gen inpre=0
replace inpre=1 if insamp08==1
replace inpre=1 if insamp10==1
replace inpre=1 if insamp12==1

gen postexp=1*(surveyyear>=expyear & expand)

forval x=1/6{
    gen expm_`x'=1*(surveyyear-expyear==-`x' & expand)
}

replace expm_6=1 if surveyyear-expyear < -6 & expand

forval x=0/3{
    gen exp`x'=1*(surveyyear-expyear==`x' & expand)
}

egen cumul_mcaid17=rowtotal(mcaid20062008 mcaid2008 mcaid20082010 mcaid2010 mcaid20102012 mcaid2012 mcaid20122014 mcaid2014 mcaid20142016 mcaid2016 mcaid20162018)
egen cumul_mcaid16=rowtotal(mcaid20062008 mcaid2008 mcaid20082010 mcaid2010 mcaid20102012 mcaid2012 mcaid20122014 mcaid2014 mcaid20142016 mcaid2016)
egen cumul_mcaid14=rowtotal(mcaid20062008 mcaid2008 mcaid20082010 mcaid2010 mcaid20102012 mcaid2012 mcaid20122014 mcaid2014)
egen cumul_mcaid12=rowtotal(mcaid20062008 mcaid2008 mcaid20082010 mcaid2010 mcaid20102012 mcaid2012)
egen cumul_mcaid10=rowtotal(mcaid20062008 mcaid2008 mcaid20082010 mcaid2010)
egen cumul_mcaid08=rowtotal(mcaid20062008 mcaid2008)

gen cumul_mcaid=.
replace cumul_mcaid=cumul_mcaid17 if surveyyear==2017
replace cumul_mcaid=cumul_mcaid16 if surveyyear==2016
replace cumul_mcaid=cumul_mcaid14 if surveyyear==2014
replace cumul_mcaid=cumul_mcaid12 if surveyyear==2012
replace cumul_mcaid=cumul_mcaid10 if surveyyear==2010
replace cumul_mcaid=cumul_mcaid08 if surveyyear==2008


gen ins2008=1-uninsured2008
gen ins2010=1-uninsured2010
gen ins2012=1-uninsured2012
gen ins2014=1-uninsured2014
gen ins2016=1-uninsured2016
gen ins2017=1-uninsured2018

gen ins20062008=1-unins20062008
gen ins20082010=1-unins20082010
gen ins20102012=1-unins20102012
gen ins20122014=1-unins20122014
gen ins20142016=1-unins20142016
gen ins20162018=1-unins20142016

egen cumul_ins17=rowtotal(ins20062008 ins2008 ins20082010 ins2010 ins20102012 ins2012 ins20122014 ins2014 ins20142016 ins2016 ins20162018)
egen cumul_ins16=rowtotal(ins20062008 ins2008 ins20082010 ins2010 ins20102012 ins2012 ins20122014 ins2014 ins20142016 ins2016)
egen cumul_ins14=rowtotal(ins20062008 ins2008 ins20082010 ins2010 ins20102012 ins2012 ins20122014 ins2014)
egen cumul_ins12=rowtotal(ins20062008 ins2008 ins20082010 ins2010 ins20102012 ins2012)
egen cumul_ins10=rowtotal(ins20062008 ins2008 ins20082010 ins2010)
egen cumul_ins08=rowtotal(ins20062008 ins2008)

gen cumul_ins=.
replace cumul_ins=cumul_ins16 if surveyyear==2017
replace cumul_ins=cumul_ins16 if surveyyear==2016
replace cumul_ins=cumul_ins14 if surveyyear==2014
replace cumul_ins=cumul_ins12 if surveyyear==2012
replace cumul_ins=cumul_ins10 if surveyyear==2010
replace cumul_ins=cumul_ins08 if surveyyear==2008

gen weight=.
replace weight=wgt2008 if surveyyear==2008
replace weight=wgt2010 if surveyyear==2010
replace weight=wgt2012 if surveyyear==2012
replace weight=wgt2014 if surveyyear==2014
replace weight=wgt2016 if surveyyear==2016
replace weight=wgt2016 if surveyyear==2017

gen earlyexp=inlist(state,10,11,25,36,50)
drop if earlyexp

gen pretrend=surveyyear-expyear
replace pretrend=0 if pretrend==.

global outcomes cumul_mcaid cumul_ins

foreach x in $outcomes {
reg `x' expm_6 expm_4 exp0 exp2 exp3 i.state i.surveyyear [aweight=weight], cluster(state)
sum `x' if expand==1 & postexp==0 [aweight=weight]
scalar ymean=r(mean)
outreg2 using "$output/hrs_firststage.xls", ctitle("`x'") addstat("pre-exp mean", ymean) append excel
}


